/** \file    PixelIterator.h
  * \brief   Allows organization process through all pixels of an image with knowledge of owning segment.
  * \date    2001
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/include/segm/PixelIterator.h,v 1.2 2005/04/04 04:10:16 cscom1r4 Exp $
  */

#ifndef _PixelIterator_h_
#define _PixelIterator_h_

#include "Segmentation.h"
#include "xtd/arr2d_wise_iterator.h"

///Allows organization process through all pixels of an image with knowledge of owning segment.
class PixelIterator
{
	typedef xtd::arr2d_wise_const_iterator<int> internal_iterator;
public:
	//default ctor
	PixelIterator() {}
	//ctor by coordinate
	PixelIterator( Segmentation &s, int x, int y ) : 
		m_s(&s), m_indexMapIt(s.segmentCoordMap(),x,y) {}
	//ctor of begin & end interators
	PixelIterator( Segmentation &s, bool begin ) : m_s(&s) 	{
		if( begin ) {
			m_indexMapIt = internal_iterator(s.segmentCoordMap(),0,0);
		} else {
			m_indexMapIt = internal_iterator(s.segmentCoordMap(),true);
		}
	}

	//segment accessors
	bool operator !() const { return *m_indexMapIt == -1; }
	Segment& operator *() { return m_s->segmentByIndex(*m_indexMapIt); }
	Segment* operator ->() { return &m_s->segmentByIndex(*m_indexMapIt); }
	int segmentIndex() const { return *m_indexMapIt; }

	//coordinate
	int x() const { return m_indexMapIt.x(); }
	int y() const { return m_indexMapIt.y(); }

	PixelIterator& operator ++() {
		++m_indexMapIt;
		return *this;
	}
	bool operator ==( const PixelIterator &x ) {
		return m_indexMapIt == x.m_indexMapIt;
	}
	bool operator !=( const PixelIterator &x ) {
		return m_indexMapIt != x.m_indexMapIt;
	}

private:
	Segmentation *		m_s;
	internal_iterator	m_indexMapIt;
};

#endif //_PixelIterator_h_
